package com.zhao.dota.controller;

import com.zhao.dota.model.Group;
import com.zhao.dota.model.LeagueUserGroup;
import com.zhao.dota.service.GroupService;
import com.zhao.dota.service.LeagueUserGroupService;
import com.zhao.dota.struct.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/group")
@RestController
public class GroupController {

    @Resource
    private GroupService groupService;
    @Resource
    private LeagueUserGroupService leagueUserGroupService;

    @GetMapping("/")
    public Result<List<Group>> list() {
        return Result.success(groupService.list());
    }

    @PostMapping("/")
    public Result<Group> save(Group group) {
        groupService.saveOrUpdate(group);
        group.setSort(group.getId());
        groupService.updateById(group);
        return Result.success(group);
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        boolean b = groupService.removeById(id);
        return Result.complete(b);
    }

    @PostMapping("/saveUserGroup")
    public Result<LeagueUserGroup> saveUserGroup(Integer leagueId, Integer userId, Integer groupId) {
        LeagueUserGroup group = leagueUserGroupService.save(leagueId, userId, groupId);
        return Result.success(group);
    }

}
